package eu.drseus.qiwidrone.android.udpsketch;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Process;
import android.view.Display;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

public class UDPSketchActivity extends Activity implements SensorEventListener {
    
	private Button button;
	private EditText editText;
	private TextView textViewRoll;
	private TextView textViewPitch;
	
    private WindowManager mWindowManager;
    private Display mDisplay;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private float mSensorX;
    private float mSensorY;
    private int mSensorXPercent;
    private int mSensorYPercent;
    private long mSensorTimeStamp;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.graph);
        

        
        //editText = (EditText) findViewById(R.id.editText_input);
        //button = (Button) findViewById(R.id.button_send);
        //button.setOnClickListener(new View.OnClickListener() {
        //    public void onClick(View v) {
         //   	try {
		//			sendUDPPackage();
		//		} catch (Exception e) {
		//			e.printStackTrace();
		//		}
        //    }
       // });
        
        //textViewRoll = (TextView) findViewById(R.id.textView_roll);
        //textViewPitch = (TextView) findViewById(R.id.textView_pitch);
        
        //startApp();
    }
    
    private void startApp(){        
        // Get an instance of the WindowManager
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mDisplay = mWindowManager.getDefaultDisplay();
        
        // Get an instance of the SensorManager
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
    }
    
    byte[] message = new byte[1500];
    
    private void sendUDPPackage() throws Exception{
    	//editText = (EditText) findViewById(R.id.editText_input);
    	String messageStr="";//editText.getText().toString();
    	int server_port = 12345;
    	DatagramSocket s = new DatagramSocket();
    	InetAddress local = InetAddress.getByName("192.168.1.101");
    	int msg_length=messageStr.length();
    	byte[] message = messageStr.getBytes();
    	DatagramPacket p = new DatagramPacket(message, msg_length,local,server_port);
    	s.send(p);
    	/*
    	String text;
    	int server_port = 12345;
    	byte[] message = new byte[1500];
    	DatagramPacket p = new DatagramPacket(message, message.length);
    	DatagramSocket s = new DatagramSocket(server_port);
    	s.receive(p);
    	text = new String(message, 0, p.getLength());
    	Log.d("Udp tutorial","message:" + text);
    	s.close();
    	*/
    }
    
    private void printSensorData(){
    	//textViewRoll.setText("Roll: "+mSensorXPercent+"%");
        //textViewPitch.setText("Pitch: "+mSensorYPercent+"%");
    	ProgressBar barX = (ProgressBar) findViewById(R.id.progressBar_roll);
    	ProgressBar barY = (ProgressBar) findViewById(R.id.progressBar_pitch);
    	barX.setProgress(mSensorXPercent);
    	barY.setProgress(mSensorYPercent);
    }

	public void onAccuracyChanged(Sensor sensor, int accuracy) {	}

	public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
            return;
        /*
         * record the accelerometer data, the event's timestamp as well as
         * the current time. The latter is needed so we can calculate the
         * "present" time during rendering. In this application, we need to
         * take into account how the screen is rotated with respect to the
         * sensors (which always return data in a coordinate space aligned
         * to with the screen in its native orientation).
         */

        switch (mDisplay.getRotation()) {
            case Surface.ROTATION_0:
                mSensorX = event.values[0];
                mSensorY = event.values[1];
                break;
            case Surface.ROTATION_90:
                mSensorX = -event.values[1];
                mSensorY = event.values[0];
                break;
            case Surface.ROTATION_180:
                mSensorX = -event.values[0];
                mSensorY = -event.values[1];
                break;
            case Surface.ROTATION_270:
                mSensorX = event.values[1];
                mSensorY = -event.values[0];
                break;
        }

        mSensorTimeStamp = event.timestamp;

        mSensorX = mSensorX + 10f;
        mSensorY = mSensorY + 10f;
        
        mSensorXPercent = (int) (mSensorX * 5f);
        mSensorYPercent = (int) (mSensorY * 5f);
        
        printSensorData();
	}
}